package com.edu;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class AlgoFrame extends JFrame {

	private int canvasWidth, canvasHeight;

	public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
		super(title);
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		
		AlgoCanvas canvas = new AlgoCanvas();
		this.setContentPane(canvas);
		this.pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public AlgoFrame(String title) {
		this(title,1024,768);
	}
	//TODO	设置数据
	private Object data;
	public void render(Object data) {
		this.data = data;
		this.repaint();
	}
	/** 
	eg:
		private Circle [] circles;
		public void render(Circle [] circles) {
			this.circles = circles;
			this.repaint();
		}
	**/
	public int getCanvasWidth() {
		return canvasWidth;
	}

	public int getCanvasHeight() {
		return canvasHeight;
	}

	private class AlgoCanvas extends JPanel {

		public AlgoCanvas() {
			super(true);
		}

		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);

			Graphics2D g2d = (Graphics2D) g;
			// 抗锯齿
			RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.addRenderingHints(hints);

			// TODO 具体绘制数据
			AlgoVisHelper.setStrokeWidth(g2d, 1);
			AlgoVisHelper.setColor(g2d, Color.RED);
			
			/**
			for (Circle circle : circles) {
				if(circle.isfilled) {
					AlgoVisHelper.fillCircle(g2d, circle.x, circle.y,circle.getR());
				} else {
					AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y,circle.getR());
				}		
			}
			**/
		}

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(canvasWidth, canvasHeight);
		}
	}
}
